package com.blue.alipay.utils.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * @描述
 * @时间 2017-6-23 12:09
 * @作者 肖亦鸣
 */
public class BaseController {
    private static Logger logger = LogManager.getLogger(BaseController.class.getName());

    @Autowired(required = false)
    protected HttpServletRequest request;

    @Autowired(required = false)
    protected HttpServletResponse response;

    /**
     * 获取参数
     *
     * @param name
     * @return
     */
    protected String getParam(String name) {
        return request.getParameter(name);
    }

    public String getParam(String name, String defaultVal) {
        String value = getParam(name);
        if (StringUtils.isNotEmpty(value)) {
            return value;
        }
        return defaultVal;
    }

    public Boolean getParamForBoolean(String name, boolean defVal) {
        String val = getParam(name);
        if (StringUtils.isNotEmpty(val)) {
            return Boolean.valueOf(val);
        }
        return defVal;
    }

    public int getParamForInt(String name, int defaultVal) {
        try {
            return Integer.valueOf(getParam(name));
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public long getParamForLong(String name, long defaultVal) {
        try {
            return Long.valueOf(getParam(name));
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public double getParamForDouble(String name, double defaultVal) {
        try {
            return Double.valueOf(getParam(name));
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public String getDomain() {
        StringBuffer url = request.getRequestURL();
        String domain = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        return domain;
    }

    public String getRequestUrl() {
        StringBuffer url = request.getRequestURL();
        if (request.getQueryString() != null) {
            url.append("?");
            url.append(request.getQueryString());
        }
        return url.toString();
    }

    public String getQueryString() {
        if (request.getQueryString() != null) {
            return "&" + request.getQueryString();
        }
        return "";
    }

    protected String getHeader(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        name = name.toLowerCase();
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            if (key.toLowerCase().equals(name)) {
                return request.getHeader(key);
            }
        }
        return null;
    }
}
